package com.ygqh.baby.controller.admin;

import com.ygqh.baby.ao.FileType;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.constant.Constant;
import com.ygqh.baby.model.ProductCombinationEditRequest;
import com.ygqh.baby.model.ProductCombinationModel;
import com.ygqh.baby.model.ProductCombinationSearchRequest;
import com.ygqh.baby.po.AdminUser;
import com.ygqh.baby.po.YgProductCombination;
import com.ygqh.baby.redis.RedisDao;
import com.ygqh.baby.service.FileService;
import com.ygqh.baby.service.WxSmallAuthorityService;
import com.ygqh.baby.service.YgProductCombinationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author admin
 */
@Controller
@RequestMapping("/admin/productCombination")
public class ProductCombinationController extends BaseController<Object, Long> {
    @Autowired
    private YgProductCombinationService ygProductCombinationService;
    @Autowired
    private RedisDao redisDao;
    @Autowired
    private WxSmallAuthorityService wxSmallAuthorityService;

    @Value("${image.domain}")
    private String domain;

    @RequestMapping("search")
    @ResponseBody
    public Map<String, Object> search(ProductCombinationSearchRequest searchRequest, HttpServletRequest request) {

        ResultSet<YgProductCombination> resultSet = ygProductCombinationService.search(searchRequest);
        return resultSet.toDatatables(null);
    }

    @RequestMapping("edit")
    @ResponseBody
    public Message editProductPack(@RequestBody ProductCombinationEditRequest editRequest) {
        AdminUser session = getSession();
        editRequest.setHandler(session.getTrueName());
        int i = ygProductCombinationService.saveOrUpdate(editRequest);
        return Message.success(i);
    }

    @RequestMapping("findInfo")
    @ResponseBody
    public Message findInfo(@RequestParam Long productCombinationId) {
        ProductCombinationModel info = ygProductCombinationService.findInfo(productCombinationId);
        return Message.success(info);
    }

    @RequestMapping("delete")
    @ResponseBody
    public Message delete(@RequestParam Long productCombinationId) {
        AdminUser session = getSession();
        int delete = ygProductCombinationService.delete(productCombinationId, session.getTrueName());
        return Message.success(delete);
    }

    @RequestMapping(value = "getSmallQRCode")
    @ResponseBody
    public Map<String, String> getSmallQRCode(Long id, String cname) {

        String redisKey = FileType.QrCode + "_small_" + id;
        String codeUrl = redisDao.get(redisKey);

        Map<String, String> map = new HashMap<>();
        if (!StringUtils.isEmpty(codeUrl)) {
            map.put("qrCode", codeUrl);
            return map;
        } else {
            String url = "pages/goods/list/list?cname=" + cname + "&productcombinationid=" + id;
            String smallQrCode = wxSmallAuthorityService.getSamllQrcode(url, 1, Constant.YIJIADIAN_PLATFORM_NO);
            redisDao.set(redisKey, smallQrCode, 60 * 60 * 1000);
            map.put("qrCode", smallQrCode);
            return map;
        }
    }
}
